package com.lvt.example.thinking.in.spring.bean.lifecycle;

import com.lvt.example.thinking.in.spring.ioc.overview.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;

/**
 * desc：Bean 元信息配置示例
 *
 * @author LT
 * @date 2022/4/5
 */
@Slf4j
public class BeanMetadataConfigDemo {

    public static void main(String[] args) {
        DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
        // 实例化
        PropertiesBeanDefinitionReader beanDefinitionReader = new PropertiesBeanDefinitionReader(defaultListableBeanFactory);

        String locationResoure = "/META-INF/user.properties";
        Resource resource = new ClassPathResource(locationResoure);
        EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
        int count = beanDefinitionReader.loadBeanDefinitions(encodedResource);

        log.info("已加载的实例数量为：{}", count);
        // 根据id和类型查找
        User user = defaultListableBeanFactory.getBean("user", User.class);
        log.info("user is:{}", user);
    }
}
